package com.shengzai.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

public class ZookeeperGetDataCode {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        Watcher watcher = new Watcher(){
            @Override
            public void process(WatchedEvent event){
                System.out.println("process函数正在运行");
            }
        };
        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181", 10000, watcher);
        Stat exists = zooKeeper.exists("/first_node", false);
        if (exists !=null){
            byte[] firstNodes = zooKeeper.getData("/first_node", false, exists);
            String s = new String(firstNodes);
            System.out.println(s);
        }else {
            System.out.println("没有找到节点");
        }
        zooKeeper.close(3000);
    }
}
